<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker clearable
                        v-model="queryParams.publishTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择发布时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          round
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['announcement:notice:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          round
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['announcement:notice:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          round
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['announcement:notice:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          round
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['announcement:notice:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="noId"/>
      <el-table-column label="标题" align="center" prop="title"/>
      <el-table-column label="内容(点击查看详情)" align="center" prop="content">
        <!--        内容做详情提示-->
        <template slot-scope="scope">
          <el-button type="text" @click="openNotice(scope)">{{ scope.row.content }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否截止" align="center" prop="ifCancel"/>
      <el-table-column label="截止时间" align="center" prop="cancelTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cancelTime, '{y}-{m}-{d}') }}</span>
          <!--          无数据时显示为空-->
          {{ scope.row.cancelTime == null ? '暂无截止时间，永久有效' : '' }}
          <el-tag type="danger" v-if="scope.row.isOverdue != '已过截止时间0天' && scope.row.isOverdue != null ">
            {{ scope.row.isOverdue}}
          </el-tag>
        </template>
      </el-table-column>
      <!--      优先级-->
      <el-table-column label="优先级" align="center" prop="priority">
        <!--        自定义标签-->
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.priority === '普通'">普通</el-tag>
          <el-tag type="warning" v-if="scope.row.priority === '加急'">加急</el-tag>
          <el-tag type="danger" v-if="scope.row.priority === '特急'">特急</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['announcement:notice:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['announcement:notice:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" maxlength="20" show-word-limit/>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!--          内容文本域-->
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" :autosize="{ minRows: 4}" maxlength="300"
                    show-word-limit/>
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker clearable
                          v-model="form.publishTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="优先级" prop="priority">
          <!--          优先级下拉框-->
          <!--          <el-input v-model="form.priority" placeholder="请输入优先级" />-->
          <el-select v-model="form.priority" placeholder="请选择">
            <el-option
              v-for="item in priorityList"
              :key="item.id"
              :label="item.name"
              :value="item.name">
            </el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="截止时间" prop="cancelTime">
          <el-date-picker clearable
                          v-model="form.cancelTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择截止时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listNotice, getNotice, delNotice, addNotice, updateNotice} from "@/api/announcement/notice";

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告管理表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        publishTime: null,
      },
      // 表单参数
      form: {},
      //优先级数据
      priorityList: [{id: 1, name: "普通",}, {id: 2, name: "加急"}, {id: 3, name: "特急"}],
      // 表单校验
      rules: {
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"},
          {max: 19, message: "最大字符应小于20个", trigger: "change"}
        ],
        content: [
          {required: true, message: "内容不能为空", trigger: "blur"},
          {max: 299, message: "最大字符应小于300个", trigger: "change"}

        ],
        publishTime: [
          {required: true, message: "发布时间不能为空", trigger: "blur"}
        ],
        priority: [
          {required: true, message: "优先级不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 公告显示详情按钮 */
    openNotice(scope) {
      this.$alert(scope.row.content, scope.row.title + ' 的内容详情', {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: scope.row.title + ` 的内容详情关闭`
          });
        }
      });
    },

    /** 查询公告管理列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noId: null,
        title: null,
        content: null,
        publishTime: null,
        ifCancel: null,
        cancelTime: null,
        priority: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公告管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noId = row.noId || this.ids
      getNotice(noId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公告管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.noId != null) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // 是否新增判断
            addNotice(this.form).then((response) => {
              if (response) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$modal.msgError("发布时间应小于截止时间，新增失败");
              }
            })
            // addNotice(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noIds = row.noId || this.ids;
      this.$modal.confirm('是否确认删除公告管理编号为"' + noIds + '"的数据项？').then(function () {
        return delNotice(noIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('announcement/notice/export', {
        ...this.queryParams
      }, `notice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
